package com.forlove.app.qselect.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.R;
import com.forlove.app.common.base.ui.BaseActivity;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.presenter.LoginPhonePresenter;
import com.forlove.app.qselect.login.view.ILoginPhoneView;
import com.forlove.app.qselect.login.widget.ErrorMessageView;

import butterknife.BindView;

/**
 * 登录输入手机号页面
 * Created by yjl on 2017/10/18.
 */

public class LoginInputPhoneActivity extends BaseActivity implements View.OnClickListener, ILoginPhoneView {

    @BindView(R.id.edittext_input_phone)
    AppCompatEditText edittext_input_phone;

    @BindView(R.id.bt_login_nextstep)
    TextView bt_login_nextstep;

    @BindView(R.id.view_error_message)
    ErrorMessageView view_error_message;

    @BindView(R.id.view_wechat_login)
    LinearLayout view_wechat_login;
    private String phoneNumber;//手机号
    private LoginEntity loginEntity;
    private LoginPhonePresenter loginPhonePresenter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_login_phone;
    }

    @Override
    public void initView() {
        loginPhonePresenter = new LoginPhonePresenter(this);
        setPresenter(loginPhonePresenter);
        initEditText();
        bt_login_nextstep.setOnClickListener(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
//        StatusBarCompat.compat(this, Color.parseColor("#ffffff"));
    }

    private void initEditText() {
        edittext_input_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    bt_login_nextstep.setBackground(getResources().getDrawable(R.drawable.bt_register_nextstep_selector));
                } else {
                    bt_login_nextstep.setBackground(getResources().getDrawable(R.drawable.bg_register_nextstep_corner_sel));
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login_nextstep:
                requestCheckPhone();
                break;
        }
    }

    private void requestCheckPhone() {
        phoneNumber = edittext_input_phone.getText().toString();
        if (TextUtils.isEmpty(phoneNumber)) {
            view_error_message.setError("请输入手机号码");
            view_error_message.setVisibility(View.VISIBLE);
            return;
        } else if (!StringUtils.isMobile(phoneNumber)) {
            view_error_message.setError("请输入正确的手机号码");
            view_error_message.setVisibility(View.VISIBLE);
            return;
        }
        loginEntity = new LoginEntity();
        loginEntity.mobile = phoneNumber;
        loginPhonePresenter.loadLoginPhoneData(loginEntity);
    }

    @Override
    public void checkPhone(final int code, final UserEntity userEntity, final String msg) {
        view_error_message.showErrorMsg(msg);
        view_error_message.showErrorMsg("请输入密码！");
        if (userEntity != null) {
            //跳转输入密码
            Intent intent = new Intent();
            intent.setClass(LoginInputPhoneActivity.this, LoginInputPWActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("userinfo", userEntity);
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            //返回年龄限制
            Toast.makeText(LoginInputPhoneActivity.this, msg, Toast.LENGTH_SHORT).show();
//            startActivity(new Intent(LoginInputPhoneActivity.this, ChoiceLimitActivity.class));
            startActivity(new Intent(LoginInputPhoneActivity.this, ChoiceNewAgeActivity.class));
            finish();
        }


    }

    @Override
    public void showErrorData(NetworkManager.ResultCode resultCode, final String msg) {
        view_error_message.showErrorMsg(msg);

    }
}
